package com.china08.yxyapi.api.manage.edu.platform.statistics.resourcepandect;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResFormat;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResFormatRepository;
import com.china08.yxyapi.service.yxyresource.statistics.ResourceTypeCountService;

import io.swagger.annotations.ApiOperation;

/**
 * 教育局平台办公 资源统计    资源类型
 * 
 */
@CrossOrigin
@RestController
public class ResourceTypeCountController extends YxyApiBaseController{
	@Autowired
	public ResourceTypeCountService resourceTypeCountService;
	
	@Autowired
	private YxyResFormatRepository yxyResFormatRepository;
	
	@ApiOperation(value = "资源统计    资源类型")
	@ResponseBody
	@RequestMapping(value = "/api_yw/manage/edu/platform/statistics/resourcepandect/resourcetype/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel precess(HttpServletRequest request, HttpServletResponse response,@RequestBody ReqModel req) {
        RespModel model = new RespModel();
        Integer schoolBureauLevel =getProfile(request, response).getSchoolBureauLevel();
        String districtCode=null;
        if (schoolBureauLevel==0) {
        	districtCode=getProfile(request, response).getSchoolAreaIdDistrict();
		}else if(schoolBureauLevel==1){
			districtCode=getProfile(request, response).getSchoolAreaIdCity();
		}else if(schoolBureauLevel==2){
			districtCode=getProfile(request, response).getSchoolAreaIdProvince();
		}
        Map<String, Object> countLst = null;
        
        DecimalFormat df = new DecimalFormat("######0.00");
        
        //饼形图
        List<Object[]>  countLstObj=new ArrayList<Object[]>();
        //名称和具体数据
        List<Object[]>  countLstObj2=new ArrayList<Object[]>();
        //调用此方法查询资源数据总览数据
        if (req.stageName == null) {			
        	countLst=resourceTypeCountService.statisticsResourceTypeCount(districtCode);
        	Integer zong=0;
        	for (String key1 : countLst.keySet()) {
        		zong+=Integer.parseInt(countLst.get(key1).toString());
			}
        	Object[]  objs=null;
        	Object[]  objs2=null;
        	for (String key : countLst.keySet()) {
        		objs=new Object[2];
        		objs2=new Object[2];
        		YxyResFormat yxyResFormat=yxyResFormatRepository.findById(key);
        		if (yxyResFormat!=null) {
        			objs[0]=yxyResFormat.getName();
    				objs2[0]=yxyResFormat.getName();
    				objs[1]=Double.parseDouble(df.format(Double.parseDouble(countLst.get(key).toString())));
            		objs2[1]=countLst.get(key).toString();
            		countLstObj.add(objs);
            		countLstObj2.add(objs2);
        		}
        		
			}
		}else{
			countLst=resourceTypeCountService.statisticsResourcePhaseCount(req.stageName,schoolBureauLevel,districtCode);
			Integer zong=0;
        	for (String key1 : countLst.keySet()) {
        		zong+=Integer.parseInt(countLst.get(key1).toString());
			}
        	Object[]  objs=null;
        	Object[]  objs2=null;
        	for (String key : countLst.keySet()) {
        		objs=new Object[2];
        		objs2=new Object[2];
        		YxyResFormat yxyResFormat=yxyResFormatRepository.findById(key);
        		
        		if (yxyResFormat!=null) {
        			objs[0]=yxyResFormat.getName();
    				objs2[0]=yxyResFormat.getName();
    				objs[1]=Double.parseDouble(df.format(Double.parseDouble(countLst.get(key).toString())));
            		objs2[1]=countLst.get(key).toString();
            		countLstObj.add(objs);
            		countLstObj2.add(objs2);
        		}
        		
        		
			}
		}
        
        model.countLstObj=countLstObj;
        model.countLstObj2=countLstObj2;
        return model;
    }
    protected static class RespModel extends YxyApiRespModel {
		//资源类型饼形图的数据
		public List<Object[]>  countLstObj;
		//名称和具体数据
		public List<Object[]>  countLstObj2;
    }
    protected static class ReqModel extends YxyApiReqModel {
        //学段名称
    	public String stageName;
    }
}
